package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IPauseOrResetMediaForPublicationView;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;
import com.cloudsigns.factorymanager2.engineering.presenter.PauseOrResetMediaForPublicationPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;


/**
 * Created by tony on 2018/6/17.
 */

public class MediaDetailForPublicationActivity extends Activity implements View.OnClickListener, IPauseOrResetMediaForPublicationView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private TextView edt_region_name, edt_media_name, edt_media_number;
    private View ll_btn;
    private TextView btn_left, btn_right;
    private boolean LEFT_CLICKABLE = false, RIGHT_CLICKABLE = false;
    private String tag = "";
    private MediaBean mediaBean;

    PauseOrResetMediaForPublicationPresenter pauseOrResetMediaForPublicationPresenter;
    private boolean hasPauseOrder;
    private String itemId, orderId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_media_detail_for_publication);

        pauseOrResetMediaForPublicationPresenter = new PauseOrResetMediaForPublicationPresenter(this);

        init();

        Intent mIntent = getIntent();
        if (null != mIntent) {
            tag = mIntent.getStringExtra(ConstantUtil.KEY_1);
            mediaBean = (MediaBean) mIntent.getSerializableExtra(ConstantUtil.KEY_2);

            if (null != mediaBean) {
                itemId = mediaBean.getId();
                orderId = mediaBean.getOrderId();

                edt_region_name.setText(mediaBean.getRegionName());
                edt_media_name.setText(mediaBean.getMediaName());
                edt_media_number.setText(mediaBean.getNumber());

                CommonEnum.MediaStatus mediaStatus = mediaBean.getStatus();
                if (mediaStatus == CommonEnum.MediaStatus.PROCESSING) {
                    ll_btn.setVisibility(View.VISIBLE);
                    btnClickable(true, false);
                } else if (mediaStatus == CommonEnum.MediaStatus.PAUSE) {
                    ll_btn.setVisibility(View.VISIBLE);
                    btnClickable(false, true);
                } else if (mediaStatus == CommonEnum.MediaStatus.PROBLEM_APPEAR) {
                    ll_btn.setVisibility(View.VISIBLE);
                    btnClickable(true, false);
                } else if (mediaStatus == CommonEnum.MediaStatus.PROBLEM_FEEDBACK) {
                    ll_btn.setVisibility(View.VISIBLE);
                    btnClickable(true, false);
                } else if (mediaStatus == CommonEnum.MediaStatus.COMPLETE_FEEDBACK) {
                    ll_btn.setVisibility(View.VISIBLE);
                    btnClickable(true, false);
                } else if (mediaStatus == CommonEnum.MediaStatus.COMPLETE) {
                    ll_btn.setVisibility(View.GONE);
                } else {
                    ll_btn.setVisibility(View.GONE);
                }
            }
        }


    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);

        edt_region_name = findViewById(R.id.edt_region_name);
        edt_media_name = findViewById(R.id.edt_media_name);
        edt_media_number = findViewById(R.id.edt_media_number);

        ll_btn = findViewById(R.id.ll_btn);
        btn_left = findViewById(R.id.btn_left);
        btn_right = findViewById(R.id.btn_right);


        tv_title.setText("上刊信息");


        rl_back.setOnClickListener(this);
        btn_left.setOnClickListener(this);
        btn_right.setOnClickListener(this);

    }


    private void btnClickable(boolean leftbool, boolean rightbool) {
        LEFT_CLICKABLE = leftbool;
        RIGHT_CLICKABLE = rightbool;

        if (leftbool) {
            btn_left.setClickable(true);
            btn_left.setBackgroundResource(R.mipmap.btn_shor_bg_blue);
        } else {
            btn_left.setClickable(false);
            btn_left.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        }

        if (rightbool) {
            btn_right.setClickable(true);
            btn_right.setBackgroundResource(R.mipmap.btn_shor_bg_red);
        } else {
            btn_right.setClickable(false);
            btn_right.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        }


    }


    private void pause() {
        btnClickable(false, false);

        hasPauseOrder = true;
        pauseOrResetMediaForPublicationPresenter.pauseOrResetMediaForPublication();
    }

    private void reset() {
        btnClickable(false, false);

        hasPauseOrder = false;
        pauseOrResetMediaForPublicationPresenter.pauseOrResetMediaForPublication();
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_left:  //暂停
                if (LEFT_CLICKABLE)
                    pause();
                break;
            case R.id.btn_right:  //恢复
                if (RIGHT_CLICKABLE)
                    reset();
                break;

        }
    }

    @Override
    public boolean gethasPauseOrder() {
        return hasPauseOrder;
    }

    @Override
    public String getitemId() {
        return itemId;
    }

    @Override
    public String getOrderId() {
        return orderId;
    }

    @Override
    public void pauseOrResetMediaForPublicationViewSuccessed() {
        if (hasPauseOrder) {
            Toast.makeText(this, "暂停成功", Toast.LENGTH_SHORT).show();


        } else {
            Toast.makeText(this, "恢复成功", Toast.LENGTH_SHORT).show();

        }

    }

    @Override
    public void pauseOrResetMediaForPublicationViewFailed(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
        if (hasPauseOrder) {
            hasPauseOrder = false;
            btnClickable(true, false);
        } else {
            hasPauseOrder = true;
            btnClickable(false, true);
        }
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
